﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using dk2Database;
using dk2Repository.Example.BL;
using dk2Repository.Example.DAL;

namespace dk2Repository.Example
{
    public partial class Form1 : Form
    {

        private string _tempfile;

        public Form1()
        {
            InitializeComponent();

            try
            {
                ((DataSet)ConfigurationManager.GetSection("system.data")).Tables[0].Rows.Add("SQLite Data Provider", ".Net Framework Data Provider for SQLite", "System.Data.SQLite", "System.Data.SQLite.SQLiteFactory, System.Data.SQLite");
            }
            catch (Exception)
            {

            }

            _tempfile = Path.GetTempFileName();
            Databases.RegisterDefault(new SqLiteDatabase(_tempfile, ""));

            User.CreateTableIfNotExists();
            Department.CreateTableIfNotExists();
            GenerateSampleData();

            dgvMain.AutoGenerateColumns = false;


            
        }

        private void GenerateSampleData()
        {
            Department d;
            d = new Department() {Name = "Dep1"};
            d.Insert();
            d = new Department() { Name = "Dep2" };
            d.Insert();

            User u;
            u = new User() { Name = "Mike", DateOfBirth = new DateTime(1950, 8, 4), Gender = User.enumGender.Male, DepartmentId = 1 };
            u.Insert();
            u = new User() { Name = "Dongel", DateOfBirth = new DateTime(1971, 1, 12), Gender = User.enumGender.Male };
            u.Insert();
            u = new User() { Name = "Beky", Gender = User.enumGender.Female, DepartmentId = 2 };
            u.Insert();
            u = new User() { Name = "Mustafa", DateOfBirth = new DateTime(1921, 10, 3), Gender = User.enumGender.Male, DepartmentId = 1 };
            u.Insert();
            u = new User() { Name = "Mandarine", DateOfBirth = new DateTime(1990, 6, 5), Gender = User.enumGender.Female, DepartmentId = 2 };
            u.Insert();
            u = new User() { Name = "Boka", Gender = User.enumGender.Male, DepartmentId = 1 };
            u.Insert();




        }

        private void Form1_Load(object sender, EventArgs e)
        {
            SelectAllUsers();
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            Databases.Dispose();
            File.Delete(_tempfile);
        }

        private void dgvMain_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            var user = GetSelectedUser();
            if (user != null)
            {
                txtName.Text = user.Name;
                dtpBirthdate.Value = user.DateOfBirth.HasValue ? user.DateOfBirth.Value : DateTime.MinValue;
                cmbSex.SelectedIndex = user.Gender == User.enumGender.Male ? 0 : 1;
            }
        }


        public void SelectAllUsers()
        {
            var users = User.SelectAll();
            SetGrid(users);
        }

        private void SetGrid(IList<User> users)
        {
            dgvMain.DataSource = users;
            dgvMain.AutoResizeColumns();

        }

        private void Insert_Click(object sender, EventArgs e)
        {
            if(string.IsNullOrEmpty(txtName.Text))
                return;

            var user = new User();
            user.Name = txtName.Text;
            user.DateOfBirth = dtpBirthdate.Value;
            user.Gender = "Male".Equals(cmbSex.Text) ? User.enumGender.Male : User.enumGender.Female;

            user.Save();

            SelectAllUsers();

        }



        private void Update_Click(object sender, EventArgs e)
        {
            var user = GetSelectedUser();
            if (user == null) 
                return;

            user.Name = txtName.Text;
            user.DateOfBirth = dtpBirthdate.Value;
            user.Gender = "Male".Equals(cmbSex.Text) ? User.enumGender.Male : User.enumGender.Female;

            user.Save();

            SelectAllUsers();
        }



        private void Delete_Click(object sender, EventArgs e)
        {
            var user = GetSelectedUser();
            if (user != null)
            {
                user.Delete();
                SelectAllUsers();
            } 

        }

        private User GetSelectedUser()
        {
            if (dgvMain.SelectedRows.Count != 1)
                return null;

            return dgvMain.SelectedRows[0].DataBoundItem as User;

        }

        private void SelectAll_Click(object sender, EventArgs e)
        {
            SelectAllUsers();
        }

        private void SelectAllWithReferences_Click(object sender, EventArgs e)
        {
            var users = User.SelectAll(true);

            SetGrid(users);
        }

        private void SelectChangeGender_Click(object sender, EventArgs e)
        {
            var users = User.SelectAllChangeGender();

            SetGrid(users);
        }

        private void SelectFemales_Click(object sender, EventArgs e)
        {
            var users = User.SelectByGender(User.enumGender.Female);

            SetGrid(users);
        }

        private void SelectUsingReader_Click(object sender, EventArgs e)
        {
            var users = User.SelectUsingReader();

            SetGrid(users);
        }

        private void SelectUsingCommand_Click(object sender, EventArgs e)
        {
            var cmd = new SelectUserCommand();
            cmd.Execute();
            var users = cmd.Result;

            SetGrid(users);
        }





        
        
    }
}
